package com.ruoyi.quality.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.quality.domain.TQualityFileItem;
import com.ruoyi.quality.service.ITQualityFileItemService;
import com.ruoyi.quality.service.ITQualityQuestionsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quality.domain.TQualityFile;
import com.ruoyi.quality.service.ITQualityFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质量问题文件Controller
 * 
 * @author mahl2
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/quality/qualityFile")
public class TQualityFileController extends BaseController
{
    @Autowired
    private ITQualityFileService tQualityFileService;

    /**
     * 查询质量问题文件列表
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:list')")
    @GetMapping("/list")
    public TableDataInfo list(TQualityFile tQualityFile)
    {
        startPage();
        List<TQualityFile> list = tQualityFileService.selectTQualityFileList(tQualityFile);
        return getDataTable(list);
    }

    /**
     * 导出质量问题文件列表
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:export')")
    @Log(title = "质量问题文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TQualityFile tQualityFile)
    {
        List<TQualityFile> list = tQualityFileService.selectTQualityFileList(tQualityFile);
        ExcelUtil<TQualityFile> util = new ExcelUtil<TQualityFile>(TQualityFile.class);



        util.exportExcel(response, list, "质量问题文件数据");
    }

    /**
     * 获取质量问题文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tQualityFileService.selectTQualityFileById(id));
    }

    /**
     * 新增质量问题文件
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:add')")
    @Log(title = "质量问题文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TQualityFile tQualityFile)
    {
        return toAjax(tQualityFileService.insertTQualityFile(tQualityFile));
    }

    /**
     * 拆分质量问题文件
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:splitFile')")
    @Log(title = "拆分质量问题文件", businessType = BusinessType.INSERT)
    @PostMapping("/splitFile")
//    public AjaxResult splitFile(@RequestBody List<TQualityFileItem> listFileItems,@RequestBody String fileId)
    public AjaxResult splitFile(@RequestBody JSONObject jo)
    {

        String fileId = jo.getString("fileId");
        List<TQualityFileItem>  listFileItems =  jo.getJSONArray("data").toJavaList(TQualityFileItem.class);
        return toAjax(tQualityFileService.splitFile(listFileItems,fileId));
    }

    /**
     * 修改质量问题文件
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:edit')")
    @Log(title = "质量问题文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TQualityFile tQualityFile)
    {
        return toAjax(tQualityFileService.updateTQualityFile(tQualityFile));
    }

    /**
     * 删除质量问题文件
     */
    @PreAuthorize("@ss.hasPermi('quality:qualityFile:remove')")
    @Log(title = "质量问题文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tQualityFileService.deleteTQualityFileByIds(ids));
    }
}
